package com.example.a10.python;

import org.junit.Test;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Component
public class PythonProcedure {

    /**
     * 调用python程序,读取训练模型，对测试数据进行预测，并得到分类结果
     */
    public int Predict(String ModelName,String TestFileName){
        String pyPath = "./py/predict.py";
        String[] cmd = new String[5];
//        cmd[0] = "../environment/venv/Scripts/python.exe"; // 指定执行命令的命令解释器
        cmd[0] = "python"; // 指定执行命令的命令解释器
        cmd[1] = pyPath;
        cmd[2] = "./model/"+ModelName; // 模型路径
        cmd[3] = "./data/test/"+TestFileName; // 测试集路径
        String[] split = ModelName.split("\\.");
        String s = split[1];
        String featureName = ModelName.replace(".txt", ".json");
        int dotIndex = TestFileName.lastIndexOf(".");
        String testName = TestFileName.substring(0, dotIndex);
        cmd[4] = "./result/"+testName+"_"+featureName;//结果保存路径

        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        BufferedReader reader = null;
        try {
            process = runtime.exec(cmd);
            int exitCode = process.waitFor();
            return exitCode;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (reader != null)
                    reader.close();
                if (process != null)
                    process.destroy();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    /**
     * 调用python程序,获取训练数据，对模型进行训练，得到训练模型
     * @param TrainFileName
     * @return 返回 0 代码执行成功,返回-1代码执行失败
     */
    public int Train(String TrainFileName){
        String pyPath = "./py/train.py";
        String[] cmd = new String[5];
//        cmd[0] = "../environment/venv/Scripts/python.exe"; // 指定执行命令的命令解释器
        cmd[0] = "python"; // 指定执行命令的命令解释器
        cmd[1] = pyPath;
        cmd[2] = "./data/train/"+TrainFileName; // 测试集路径
        cmd[3] = "true";
        cmd[4]= "0";
        // 可以添加更多参数，例如：cmd[3] = "arg2"; cmd[4] = "arg3"; ...

        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        BufferedReader reader = null;
        try {
            process = runtime.exec(cmd);
            int exitCode = process.waitFor();
            return 0;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (reader != null)
                    reader.close();
                if (process != null)
                    process.destroy();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    /**
     * 获取特征重要性
     * @param modelName
     * @return
     */
    public int Feature(String modelName,String TestName){
        String pyPath = "./py/getfeatureimportance.py";
        String testPath = "./data/test/"+TestName;
        String[] cmd = new String[6];
//        cmd[0] = "../environment/venv/Scripts/python.exe"; // 指定执行命令的命令解释器
        cmd[0] = "python"; // 指定执行命令的命令解释器
        cmd[1] = pyPath;
        cmd[2] = "./model/"+modelName;
        cmd[3] = testPath;
        String[] split = modelName.split("\\.");
        String s = split[1];
        String featureName = modelName.replace(".txt", ".json");
        cmd[4] = "./feature/"+"lgb_"+featureName;
        cmd[5] = "./feature/"+"shap_"+featureName ;

        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        BufferedReader reader = null;
        try {
            process = runtime.exec(cmd);
            int exitCode = process.waitFor();
            return 0;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (reader != null)
                    reader.close();
                if (process != null)
                    process.destroy();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

}
